package cn.zmall.manager.controller;

import cn.zmall.common.pojo.Result;
import cn.zmall.common.pojo.TreeNode;
import cn.zmall.common.utils.ResultUtil;
import cn.zmall.manager.pojo.TbItemCat;
import cn.zmall.manager.service.ItemCatService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author BinBini
 * @Date 2020/12/21 12:28
 * @Version 1.0
 */
@RestController
@Api(value = "商品分类")
public class ItemCatController {

    @Reference
    private ItemCatService itemCatService;

    @RequestMapping(value = "/item/cat/list", method = RequestMethod.GET)
    @ApiOperation(value = "通过父ID获取商品分类列表")
    public Result<List<TreeNode>> getItemCatList(@RequestParam(name="id",defaultValue = "0") int parentId) {
        List<TreeNode> result = itemCatService.getItemCatList(parentId);
        return new ResultUtil<List<TreeNode>>().setData(result);
    }

    @RequestMapping(value = "/item/cat/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加商品分类")
    public Result<Object> addItemCategory(@ModelAttribute TbItemCat tbItemCat) {
        int result = itemCatService.addItemCat(tbItemCat);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/item/cat/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑商品分类")
    public Result<Object> updateItemCategory(@ModelAttribute TbItemCat tbItemCat) {
        int result = itemCatService.updateItemCat(tbItemCat);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/item/cat/del/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除商品分类")
    public Result<Object> deleteItemCategory(@PathVariable Long id) {
        itemCatService.deleteItemCat(id);
        return new ResultUtil<Object>().setData(null);
    }

}
